package com.qf.job;

import com.qf.constant.CacheConstants;
import com.qf.constant.RabbitMQConstants;
import com.qf.service.CacheService;
import com.qf.service.MailService;
import com.rabbitmq.client.Channel;
import lombok.extern.slf4j.Slf4j;
import org.apache.shardingsphere.elasticjob.api.ShardingContext;
import org.apache.shardingsphere.elasticjob.simple.job.SimpleJob;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.util.Set;

/**
 * @author lenovo
 * @version V1.0
 * @Date 2021/11/26 12:37
 */
@Component
@Slf4j
public class MonitorQueueMessageJob implements SimpleJob {

    @Autowired
    private Channel channel;

    @Autowired
    private CacheService cacheService;

    @Autowired
    private MailService mailService;

    @Override
    public void execute(ShardingContext shardingContext) {
        //1.监听网关队列
        gatewayQueue();

        //2.监听其他队列
        //获取队列名
        // 发送短信的消息队列名  ---  接口模块发送消息到策略模块
         String TOPIC_PRE_SEND = "pre_send_sms_topic";
        //下发日志TOPIC   ---   写日志到ES的队列名
        String TOPIC_SMS_SEND_LOG = "sms_send_log_topic";
        //推送状态报告TOPIC    ---      让接口模块给客户端一个推送.
         String TOPIC_PUSH_SMS_REPORT = "push_sms_report_topic";
        //状态报告更新TOPIC    ----  修改日志到ES
        String TOPIC_UPDATE_SMS_REPORT = "report_update_topic";
        //过滤器更新TOPIC
        String TOPIC_FILTER_UPDATE = "sms_filters_update_exchange";

        queue(TOPIC_PRE_SEND,
              TOPIC_SMS_SEND_LOG,
              TOPIC_PUSH_SMS_REPORT,
              TOPIC_UPDATE_SMS_REPORT,
              TOPIC_FILTER_UPDATE);

    }

    private void gatewayQueue() {
        //1.获取所有通道id用来得到队列名
        Set<String> channelIds = cacheService.setGet(CacheConstants.CACHE_PREFIX_SMS_CHANNEL_ALL);
        for (String channelId : channelIds) {
            //2.获取到队列名
            String queueName = RabbitMQConstants.TOPIC_SMS_GATEWAY + channelId;
            queue(queueName);
        }
    }

    /**
     * 监听队列，参数数队列名
     * @param queueNames
     */
    private void queue(String... queueNames){
        for (String queueName : queueNames) {
            int messageCount = 0;
            try {
                //TODO 使用channel创建队列的参数
                //3.创建队列(如果队列不存在)
                channel.queueDeclare(queueName, true, false, false, null);
                //4.获取队列中的消息数量
                messageCount = channel.queueDeclarePassive(queueName).getMessageCount();

            } catch (IOException e) {
                e.printStackTrace();
            }

            //5.判断消息数量
            if (messageCount > 5){
                //发送邮件
                log.info("【检测模块 - 检测队列】发现消息堆积，消息数量 messageCount={},队列名queueName={}",messageCount,queueName);
                mailService.send("【检测模块 - 检测队列】发现消息堆积，消息数量 :" + messageCount +",队列名: "+queueName);
            }else {
                log.info("【检测模块 - 检测队列】消息队列状态良好！！！  队列名={}",queueName);
            }
        }

    }


}